Skip to main content

Create dummy variable

Dummy variables are often used in regression analysis. Such variables need to be numerical and take the values 0 and 1.

 //Connect to datastore
require no.ssb.fdb:32 as db

textblock
Example 1:
Coding of the dummy "man" - traditional method: First, all values ​​are set to 0. The command `replace` then replaces the value 0 with 1 where the variable gender has the value "1".
endblock

create-dataset demography1
import db/BEFOLKNING_KJOENN as gender

//Create a dummy variable that indicates males based on the variable gender
generate male = 0
replace male = 1 if gender == '1'

tabulate male


textblock
Example 2:
Coding the dummy "man" - condensed method: By setting up the expression in a specific way, you can do everything in one step by the `generate` command. The value 1 is given to observations that satisfy the condition you set. The second value (0) is automatically set for all observations that *do not* satisfy the condition. This avoids coding in two operations, and missing values are not set to the value 0.
endblock

create-dataset demography2
import db/BEFOLKNING_KJOENN as gender

//Create a dummy variable that indicates male based on the variable gender
generate male = gender == '1'

tabulate male